package com.hl233.framework.controller;

import com.hl233.framework.common.Constants;
import com.hl233.framework.entity.KafkaDataEntity;
import com.hl233.framework.service.KafkaDataService;
import com.hl233.framework.utils.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.kafka.annotation.PartitionOffset;
import org.springframework.kafka.annotation.TopicPartition;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * @author 张振强15065206763
 * @create 2024-09-03
 */
@RestController
@RequestMapping("/co")
public class ConsumerTest {

    private static Integer count = 0;

    @Autowired
    private KafkaDataService kafkaDataService;

//    @KafkaListener(topics = Constants.HL233_DEVICE_MSG_TOPIC)
//    @KafkaListener(topicPartitions = {@TopicPartition(topic = Constants.HL233_DEVICE_MSG_TOPIC,partitions = {"0"})})
//    @KafkaListener(groupId = "topic_group_test",topicPartitions = { @TopicPartition(topic = "topic_test",partitionOffsets = @PartitionOffset(partition = "0", initialOffset = "9830")) })
    public void kafkaConsumer0(String result) throws Exception {
        count++;
        System.out.println("消费者1---------------->消费一次数据"+count);
        System.out.println("消费到消息" + result +"------------"+DateUtil.dateToString(new Date(), DateUtil.FMT_Y_M_D_H_M_S));
        KafkaDataEntity entity = new KafkaDataEntity();
        entity.setContext(result);
        entity.setCreatetime(DateUtil.dateToString(new Date(), DateUtil.FMT_Y_M_D_H_M_S));
        entity.setVal(count);
        kafkaDataService.save(entity);

    }

//    @KafkaListener(topics = Constants.HL233_DEVICE_MSG_TOPIC)
    public void kafkaConsumer1(String result) throws Exception {
        count++;
        System.out.println("消费者2---------------->消费一次数据"+count);
        System.out.println("消费到消息" + result +"------------"+DateUtil.dateToString(new Date(), DateUtil.FMT_Y_M_D_H_M_S));
        KafkaDataEntity entity = new KafkaDataEntity();
        entity.setContext(result);
        entity.setCreatetime(DateUtil.dateToString(new Date(), DateUtil.FMT_Y_M_D_H_M_S));
        entity.setVal(count);
        kafkaDataService.save(entity);

    }
}
